package com.zkingsoft.services.distribution.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.dao.distribution.DisDesignerDao;
import com.zkingsoft.dao.distribution.DisWithdrawalsFlowDao;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.distribution.DisDesigner;
import com.zkingsoft.model.distribution.DisWithdrawalsFlow;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisWithdrawalsFlowService;
import com.zkingsoft.util.ModelUtils;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-06-26 07:15
 */
@Service("disWithdrawalsFlowService")
public class DisWithdrawalsFlowServiceImpl implements DisWithdrawalsFlowService {

	@Autowired
	private DisWithdrawalsFlowDao disWithdrawalsFlowDao;
	
	@Autowired
	private DisDesignerDao disDesignerDao;

	@Override
	public int add(DisWithdrawalsFlow disWithdrawalsFlow) {

		return disWithdrawalsFlowDao.insert(disWithdrawalsFlow);

	}

	@Override
	public int batchAdd(List<DisWithdrawalsFlow> disWithdrawalsFlowList) {
		int num = 0;

		int c = 10000;
		int size = disWithdrawalsFlowList.size() / c + 1;
		for (int i = 0; i < size; i++) {
			int begin = i * c;
			int end = (i + 1) * c;
			end = end >= disWithdrawalsFlowList.size() ? disWithdrawalsFlowList
					.size() : end;
			List<DisWithdrawalsFlow> insertList = disWithdrawalsFlowList
					.subList(begin, end);
			num += disWithdrawalsFlowDao.batchInsert(insertList);
		}
		return num;

	}

	@Override
	public int modifyByMap(DisWithdrawalsFlow oldDisWithdrawalsFlow,
			DisWithdrawalsFlow newDisWithdrawalsFlow) {

		if (!ModelUtils
				.isModified(oldDisWithdrawalsFlow, newDisWithdrawalsFlow)) {
			return 1;
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldDisWithdrawalsFlow,
					newDisWithdrawalsFlow);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("flowId", oldDisWithdrawalsFlow.getFlowId());
			disWithdrawalsFlowDao.updateByMap(modifyMap);
		}
		return 1;
	}

	@Override
	public int modifyByModel(DisWithdrawalsFlow disWithdrawalsFlow) {

		return disWithdrawalsFlowDao.updateByModel(disWithdrawalsFlow);

	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public int remove(List<Long> list) {
		for(Long id : list) {
			DisWithdrawalsFlow flow = disWithdrawalsFlowDao.selectById(id);
			//未支付的 返回给设计师
			if("1".equals(flow.getFlowState())) {
				DisDesigner disDesigner = disDesignerDao.selectById(flow.getDesId());
				if(disDesigner != null) {
					disDesigner.setDesRebate(disDesigner.getDesRebate().add(flow.getFlowMoney()));
					disDesignerDao.updateByModel(disDesigner);
				}
			}
		}
		return disWithdrawalsFlowDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long flowId) {

		return disWithdrawalsFlowDao.deleteById(flowId);

	}

	@Override
	public int removeByModel(DisWithdrawalsFlow disWithdrawalsFlow) {

		return disWithdrawalsFlowDao.deleteByModel(disWithdrawalsFlow);

	}

	@Override
	public List<DisWithdrawalsFlow> findInPage(
			DisWithdrawalsFlow disWithdrawalsFlow, PaginationVO pageVo) {

		return disWithdrawalsFlowDao.selectInPage(disWithdrawalsFlow, pageVo);

	}

	@Override
	public List<DisWithdrawalsFlow> findByModel(
			DisWithdrawalsFlow disWithdrawalsFlow) {

		return disWithdrawalsFlowDao.selectByModel(disWithdrawalsFlow);

	}

	@Override
	public int findTotal(DisWithdrawalsFlow disWithdrawalsFlow) {

		return disWithdrawalsFlowDao.selectTotalRecord(disWithdrawalsFlow);

	}

	@Override
	public DisWithdrawalsFlow findById(Long flowId) {

		return disWithdrawalsFlowDao.selectById(flowId);

	}

}